package experiment4.task1;


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 冯永龙
 * @date 2021/4/5 9:48
 */
public class PersonDisplay implements PersonAction{
    private final List<Person> personList;

    public PersonDisplay() {
        PersonInput personInput = new PersonInput();
        this.personList = personInput.createPerson();
    }

    public void display(){
        System.out.println("输出:");
        for (Person person : personList) {
            process(person);
        }
    }

    @Override
    public Person process(Person person) {
        Class<? extends Person> personClass = person.getClass();
        Field[] fields = personClass.getDeclaredFields();
        for (Field field : fields) {
            Label label = field.getAnnotation(Label.class);
            if (label != null){
                System.out.print(label.value());
            }
            String name = field.getName();
            String methodName = "get"+name.substring(0,1).toUpperCase()+name.substring(1);
            Object invoke;
            try {
                Method method = personClass.getMethod(methodName);
                invoke = method.invoke(person, null);
                System.out.println(invoke);
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return person;
    }
    static class  test{
        public static void main(String[] args) {
            PersonAction personDisplay = new PersonDisplay();
            ((PersonDisplay) personDisplay).display();
        }
    }
}
